// sharing/pages/user/address/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  showForm: false,
	  address: [],
	  idx: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	var that = this;
	app.getUserInfo(function(openid){
		app.util.request({
			url: 'entry/wxapp/adrList',
			data: { openid: openid, m: 'junsion_wifinav'},
			cachetime: 0,
			success: function (res) {
				if(res.data.code==1) that.setData({list: res.data.list});
			}
		});
	});
  
  },
  addAdr: function(){
	  this.setData({address: [], showForm: true });
  },
  confirmSubmit: function(e){
    var data = {
		nickname: e.detail.value.u_name,
		mobile: e.detail.value.u_phone,
		gender: e.detail.value.u_sex,
		address: e.detail.value.u_address,
		introduce: e.detail.value.u_introduce,
		m: 'junsion_wifinav'
	};
	wx.showLoading({title: '请稍后...'});
	app.util.request({
		url: 'entry/wxapp/updateUser',
		data: data,
		cachetime: 0,
		success: function (res) {
			wx.hideLoading();
			wx.showToast({
			  title: '资料更新成功',
			  icon: 'success',
			  duration: 1500
			});
		}
	});
  },
  cancel: function(){
	  this.setData({showForm: false });
  },
  editAdr: function(e){
	  var idx = e.currentTarget.dataset.idx;
	  var list = this.data.list;
	  this.setData({address: list[idx], idx: idx, showForm: true });
  },
  delAdr: function(e){
	  var that = this;
	  var list = that.data.list;
	  var idx = e.currentTarget.dataset.idx;
	  wx.showModal({
		  title: '温馨提示',
		  content: '确定删除吗？',
		  showCancel: false,
		  success: function(r){
			  if(r.confirm){
			    app.util.request({
					url: 'entry/wxapp/delAdr',
					data: { id: list[idx]['id'], m: 'junsion_wifinav'},
					cachetime: 0,
					success: function (res) {
						if(res.data==1){
							list.splice(idx,1);
							that.setData({list: list});
						}
					}
				});
			  }
		  }
	  });
  }
})